using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FileInstruction:InstructionBase
    {
        #region props
        private string _SrcPath;
        /// <summary></summary>
        public string SrcPath
        {
            get { return _SrcPath; }
            set { _SrcPath = value; }
        }

        private string _TgtPath;
        /// <summary></summary>
        public string TgtPath
        {
            get { return _TgtPath; }
            set { _TgtPath = value; }
        }

        private FileActionType _ActionType;
        /// <summary></summary>
        public FileActionType ActionType
        {
            get { return _ActionType; }
            set { _ActionType = value; }
        }

        private bool _OverWrite;
        /// <summary></summary>
        public bool OverWrite
        {
            get
            {
                return this._OverWrite;
            }
            set
            {
                this._OverWrite = value;
            }
        }
        #endregion

        /// <summary></summary>
        public FileInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcPath = string.Empty;
            this._TgtPath = string.Empty;
            this._ActionType = FileActionType.Move;
        }

        /// <summary></summary>
        public FileInstruction(Guid componentID, Guid jobTicketID, string srcPath, string tgtPath, FileActionType actionType) : base(componentID, jobTicketID)
        {
            this._SrcPath = srcPath;
            this._TgtPath = tgtPath;
            this._ActionType = actionType;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode instructionNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "SourcePath", this.SrcPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "TargetPath", this.TgtPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "FileActionType", ((int) this.ActionType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,instructionNode,"OverWrite",this.OverWrite.ToString());

            return instructionNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileInstruction instruction = new FileInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.SrcPath = xNode.Attributes.GetNamedItem("SourcePath").Value;
                instruction.TgtPath = xNode.Attributes.GetNamedItem("TargetPath").Value;
                instruction.ActionType =
                    (FileActionType) int.Parse(xNode.Attributes.GetNamedItem("FileActionType").Value);
                instruction.OverWrite = bool.Parse(xNode.Attributes.GetNamedItem("OverWrite").Value);

                return instruction;
            }
            else
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
        }
    }
}
